package com.honey.eclipse.formatter;

import java.io.File;
import java.util.Properties;

import org.w3c.dom.Element;

import com.honey.configureation.ConfigurationPlugin;
import com.honey.configureation.holder.ConfigurationHolder;
import com.honey.configureation.xmlparser.ParseElement;
import com.honey.core.NullPlugin;
import com.honey.core.utils.ObjectFactory;

public class EclipseFormatterPlugin extends NullPlugin implements ConfigurationHolder{

	private String camelCodeFormatter  = null;
	
	private String cStyleFormatter  = null;
	
	private String eclipseCodeFormatter  = null;
	
	private Style style = Style.SPRING; 
	
	
	@Override
	protected void doStart() throws Exception {
		ConfigurationPlugin cp = (ConfigurationPlugin)ObjectFactory.getRequestPlugin(this, "honey.configuration.plugin");
		String path =cp.getContextInstant().getSystemProperties().getProperty("application.local.path")+File.separator+"plugins"+File.separator+"honey.eclipse.formatter" + File.separator;
		camelCodeFormatter = this.getDescriptor().getAttribute("CamelCodeFormatter").getValue();
		if( camelCodeFormatter != null){
			camelCodeFormatter =path + camelCodeFormatter;
		}
		cStyleFormatter = this.getDescriptor().getAttribute("CStyleFormatter").getValue();
		if( cStyleFormatter != null){
			cStyleFormatter =path + cStyleFormatter;
		}
		
		eclipseCodeFormatter = this.getDescriptor().getAttribute("EclipseCodeFormatter").getValue();
		if( eclipseCodeFormatter != null){
			eclipseCodeFormatter =path + eclipseCodeFormatter;
		}
		
	}
	
	@Override
	public void parserElement(Element element, Properties properties) {
		String tmp =null;
		tmp =  ParseElement.getConfiguration(element, "formatter", properties);	
		if("camel".equalsIgnoreCase(tmp) ){
			style = Style.CAMEL; 
		}else if("cStyle".equalsIgnoreCase(tmp) ){
			style = Style.C; 
		}else if("c".equalsIgnoreCase(tmp) ){
			style = Style.C; 
		}else if("c++".equalsIgnoreCase(tmp) ){
			style = Style.C; 
		}else if("c/c++".equalsIgnoreCase(tmp) ){
			style = Style.C; 
		}else if("spring".equalsIgnoreCase(tmp) ){
			style = Style.SPRING; 
		}else if("default".equalsIgnoreCase(tmp) ){
			style = Style.DEFAULT; 
		}
	}

	@Override
	public String toXmlElement() {
		return null;
	}

	@Override
	public void validate() {
		
	}

	public String getStyleStettingFile(){
		String answer = null;
		switch( style ){
			case C:
				answer = cStyleFormatter;
			break ;
			
			case SPRING:
				answer = eclipseCodeFormatter;
			
			case CAMEL:
				answer = camelCodeFormatter;
			break ;	
		}
		
		return answer;
	}
	
	public Style getStyle() {
		return style;
	}
	
}
